package com.example.administrator.xxnapp.fragment.tucao;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.TuCaoBean;
import com.example.administrator.xxnapp.http.HttpUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.mdruby.baselibrary.utils.DateUtil;

/**
 * Created by Went_Gone on 2018/4/28.
 */

public class RVTuCaoAdapter extends RecyclerView.Adapter {
    private static final String TAG = "RVTuCaoAdapter";
    private Context context;
    private List<TuCaoBean> mDatas;

    public RVTuCaoAdapter(Context context, List<TuCaoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RVTuCaoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tucao_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RVTuCaoViewHolder){
            ((RVTuCaoViewHolder) holder).bindView(position);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas==null?0:mDatas.size();
    }

    private class RVTuCaoViewHolder extends RecyclerView.ViewHolder{
        private ImageView mIVHead;
        private TextView mTVName,mTVDate,mTVContent;
        private TextView mTVPraise,mTVComment;
        private RecyclerView mRV,mRVComment;

        public RVTuCaoViewHolder(View itemView) {
            super(itemView);
            mIVHead = itemView.findViewById(R.id.item_tucao_layout_IV);
            mTVName = itemView.findViewById(R.id.item_tucao_layout_TV_Name);
            mTVDate = itemView.findViewById(R.id.item_tucao_layout_TV_Time);
            mTVContent = itemView.findViewById(R.id.item_tucao_layout_TV_Content);
            mTVPraise = itemView.findViewById(R.id.item_tucao_layout_TV_praise);
            mTVComment = itemView.findViewById(R.id.item_tucao_layout_TV_comment);
            mRV = itemView.findViewById(R.id.item_tucao_layout_RV);
            mRVComment = itemView.findViewById(R.id.item_tucao_layout_RV_Comment);
            mRV.setLayoutManager(new GridLayoutManager(context,3));
            mRVComment.setLayoutManager(new LinearLayoutManager(context));
        }

        private void bindView(final int position){
            TuCaoBean bean = mDatas.get(position);
            RequestOptions options = new RequestOptions();
            options.circleCrop();
            options.error(R.mipmap.ic_launcher_round);
            Log.e(TAG, "bindView: " +HttpUrl.BASE_URL_BASE+bean.getHeadImage());
            Glide.with(context).load(HttpUrl.BASE_URL_BASE+bean.getHeadImage()).apply(options).into(mIVHead);
            mTVName.setText(bean.getUserName());
            mTVDate.setText(DateUtil.getDate(bean.getCreateDate(),"yyyy-MM-dd HH:mm"));
            mTVContent.setText(bean.getContentString());
            mRV.setAdapter(new RVTuCaoImageShowAdapter(context,bean.getImageList()));

            mTVPraise.setText(bean.getPraise()==0?"赞":"已赞");

            mTVPraise.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onTuCaoItemClickListener != null){
                        onTuCaoItemClickListener.praiseClick(position);
                    }
                }
            });

            mTVComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onTuCaoItemClickListener != null){
                        onTuCaoItemClickListener.commentClick(position);
                    }
                }
            });
            RVCommentAdapter adapter = new RVCommentAdapter(context, bean.getCommentList());
            adapter.setOnCommentItemClickListener(new RVCommentAdapter.OnCommentItemClickListener() {
                @Override
                public void onItemUserClick(int childPosition, int userId) {
                    if (onTuCaoItemClickListener != null){
                        onTuCaoItemClickListener.onItemUserClick(position,childPosition,userId);
                    }
                }

                @Override
                public void onItemClick(int childPosition, int commentType) {
                    if (onTuCaoItemClickListener != null){
                        onTuCaoItemClickListener.onItemClick(position,childPosition,commentType);
                    }
                }
            });
            mRVComment.setAdapter(adapter);

        }
    }

    private OnTuCaoItemClickListener onTuCaoItemClickListener;

    public void setOnTuCaoItemClickListener(OnTuCaoItemClickListener onTuCaoItemClickListener) {
        this.onTuCaoItemClickListener = onTuCaoItemClickListener;
    }

    public interface OnTuCaoItemClickListener{
        void praiseClick(int position);
        void commentClick(int position);
        void onItemUserClick(int groupPosition,int childPosition,int userId);
        void onItemClick(int groupPosition,int childPosition,int commentType);
    }
}
